let redis = require("../util/redisDB")
const util = require('../util/common')
const crypto = require('crypto')

exports.articleTalk = (req, res, next) => {
    if ('a_id' in req.body && 'talk' in req.body) {
        // 组织字符串
        let talk = {
            talk: req.body.talk,
            time: Date.now(),
            username: req.username
        }
        let key = req.headers.fapp + ':article:' + req.body.a_id + ':talk'
        redis.get(key).then((data) => {
            let tData = []
            if (data) {
                tData = data
            } else {
                tData.push(talk)
            }
            redis.set(key, tData)
            res.json(util.getReturnData(0, '评论成功'))
        })
    } else { 
        res.json(util.getReturnData(1,'评论错误'))
    }
}

exports.getUserInfo = (req, res, next) => { 
    redis.get(req.headers.fapp + ":user:info:" + req.params.username).then((data) => {
        if (data) {
            if (req.params.username == req.username) {
                //自己的资料
                delete data.password
            } else {
                // 他人的资料 通过username查找
                delete data.phone
                delete data.password
            }
            res.json(util.getReturnData(0, '', data))
        } else { 
            res.json(util.getReturnData(1,'用户不存在'))
        }
    })
}